namespace PickGold.Charting.Design
{
    using System;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;

    internal class AnnotationAxisNameTreeView : TreeView
    {
        private Annotation _annotation;
        private Axis _axis;
        private IWindowsFormsEditorService _edSvc;
        private bool _showXAxes = true;

        public AnnotationAxisNameTreeView(IWindowsFormsEditorService edSvc, Annotation annotation, Axis axis, bool showXAxes)
        {
            this._annotation = annotation;
            this._axis = axis;
            this._edSvc = edSvc;
            this._showXAxes = showXAxes;
            base.BorderStyle = BorderStyle.None;
            this.FillTree();
        }

        private void FillTree()
        {
            bool flag = false;
            base.BeginUpdate();
            TreeNode node = base.Nodes.Add("NotSet");
            if (((this._annotation != null) && (this._annotation.AnnotationGroup == null)) && (this._annotation.Chart != null))
            {
                foreach (ChartArea area in this._annotation.Chart.ChartAreas)
                {
                    TreeNode node2 = base.Nodes.Add(area.Name);
                    node2.Tag = area;
                    foreach (Axis axis in area.Axes)
                    {
                        if ((((axis.AxisName != AxisName.Y) && (axis.AxisName != AxisName.Y2)) || !this._showXAxes) && (((axis.AxisName != AxisName.X) && (axis.AxisName != AxisName.X2)) || this._showXAxes))
                        {
                            TreeNode node3 = node2.Nodes.Add(axis.Name);
                            node3.Tag = axis;
                            if (this._axis == axis)
                            {
                                node2.Expand();
                                base.SelectedNode = node3;
                                flag = true;
                            }
                        }
                    }
                }
            }
            if (!flag)
            {
                base.SelectedNode = node;
            }
            base.EndUpdate();
        }

        public Axis GetNewValue()
        {
            if (((base.SelectedNode != null) && (base.SelectedNode.Tag != null)) && (base.SelectedNode.Tag is Axis))
            {
                return (Axis) base.SelectedNode.Tag;
            }
            return null;
        }

        protected override void OnDoubleClick(EventArgs e)
        {
            base.OnDoubleClick(e);
            if (this._edSvc != null)
            {
                if (this.GetNewValue() != null)
                {
                    this._edSvc.CloseDropDown();
                }
                else if ((base.SelectedNode != null) && (base.SelectedNode.Text == "NotSet"))
                {
                    this._edSvc.CloseDropDown();
                }
            }
        }
    }
}

